package com.huxin.servelt.admin;

import com.alibaba.fastjson.JSON;
import com.huxin.pojo.*;
import com.huxin.service.Userservice;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Date;
import java.util.List;

@WebServlet("/admin/comfirm")
public class ComfirmbooksServlet extends HttpServlet {
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取请求方法
        req.setCharacterEncoding("utf-8");
        //处理响应乱码
        resp.setContentType("text/json;charset=utf-8");
        //获取id值
        Integer bid = Integer.valueOf(req.getParameter("bid"));
        Userservice service = new Userservice();
        List<Bookreservation> bookreservations=service.selectAllreservation();
        Bookreservation bean=null;
        //查找bookreservation表中对应的信息
        for (Bookreservation b : bookreservations) {
            if (bid==b.getBid()){
                bean=b;
            }
        }
        //从预约表中删除数据
        int i=service.delectereservation(bid);
        //拿到预约人的id
        int aid=bean.getAid();
        //通过bid在查找书的信息
        BooksInfo booksInfo=service.selectBookById(bid);
        //查询用户的信息
        UserInfo userInfo = service.selectById(aid);
        //在借阅表内加入信息
        BookHisInfo bookHisInfo = BookHisInfo.builder()
                .bid(bean.getBid())//书本id
                .book_name(bean.getBook_name())//书本名
                .admin_name(bean.getAdmin_name())//用户名
                .aid(bean.getAid())
                .card(bean.getCard())
                .username(bean.getUsername())
                .status(1)//未还书状态
                .begin_time(new Date(System.currentTimeMillis()))//借书的日期
                .end_time(new Date(System.currentTimeMillis()+1000*60*60*24*(userInfo.getLend_num())))//还书的日期 在借书的日期上往后推用户的最长借书时间
                .build();
        int j=service.addhisinfo(bookHisInfo);
        if(i>0&&j>0){
                    //借书成功，减少用户最大借书数量
                    int i1=service.decreaseUserNum(aid);
                    //图书数量减少
//                    int i2=service.decreaseBookNum(booksInfo.getBid());
                    //增加用户借书总数 最佳读者排行要用
                    int i3=service.addFrequency(aid);
                    //增加该书籍被借阅次数
                    service.addBookHisCard(bid);
                    if (i1==1){
                        resp.getWriter().write("1");//借书成功
                    }
                      else {
                        resp.getWriter().write("2");//借书失败
                    }

        }
        else resp.getWriter().write("2");//借书失败


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
